﻿using ConfExpert.Forms.Base;
using DevExpress.Utils.Win;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;


using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Araclar
{
    public partial class TaskList : XForm
    {
        public TaskList()
        {
            InitializeComponent();
        }

        BindingSource bs1;
        private void TaskList_Load(object sender, EventArgs e)
        {
            

            TAM.TaskListTableAdapter = new Datasets.MyDataTableAdapters.TaskListTableAdapter();

            GetDetails("TaskList", "GetTaskList", string.Empty);
            bs1 = new BindingSource(ds, "TaskList");
            AddBsToList(bs1);

            //bs1.CurrentChanged += bs1_CurrentChanged;
            
            InitGridView(gridView1, bs1);
            //memoEdit1.DataBindings.Add("EditValue", bs1, "Aciklama");
            gridView1.ShowFindPanel();

            //gridView1.Init("GetTaskList");
            gridView1.YanYanaGetir("Sira:TaskListId");
            gridView1.MakeColumnInvisible("TaskListId");
            gridView1.Columns["Aciklama"].ColumnEdit = new RepositoryItemMemoExEdit();
            ((RepositoryItemMemoExEdit)gridView1.Columns["Aciklama"].ColumnEdit).ShowIcon = false;

            ((RepositoryItemMemoExEdit)gridView1.Columns["Aciklama"].ColumnEdit).PopupFormMinSize = new Size(500, 300);
            //((RepositoryItemMemoExEdit)gridView1.Columns["Aciklama"].ColumnEdit).Popup += TaskList_Popup;
            setgrid(gridView1);

            bbiKaydet.ItemClick += bbiKaydet_ItemClick;
            ds.TaskList.TableNewRow += TaskList_TableNewRow;

            gridView1.BestFitColumns();
            gridView1.Columns["Aciklama"].Width = 500;

            if (!ytk(75))
                bbiKaydet.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
        }

        void TaskList_Popup(object sender, EventArgs e)
        {
            (sender as IPopupControl).PopupWindow.Width = (sender as BaseEdit).Width;
        }

        void TaskList_TableNewRow(object sender, System.Data.DataTableNewRowEventArgs e)
        {
            int maxid = ds.TaskList.Compute("MAX(Sira)", string.Empty).ToInt();
            e.Row["Sira"] = maxid + 1;
        }

        string OrderFieldName = "Sira";
        void setgrid(GridView gv)
        {
            gv.Columns[OrderFieldName].SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            gv.OptionsCustomization.AllowSort = false;
            gv.OptionsView.ShowGroupPanel = false;
            gv.KeyDown += gridView1_KeyDown;
            gv.OptionsBehavior.ImmediateUpdateRowPosition = true;
            gv.OptionsSelection.EnableAppearanceFocusedRow = true;
            gv.Appearance.FocusedRow.BackColor = gv.Appearance.FocusedRow.BackColor2 = Color.YellowGreen;
            gv.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            gv.AddSummary();

            //gv.Appearance.FocusedRow.BackColor =
            //gv.Appearance.SelectedRow.BackColor =
            //gv.Appearance.HideSelectionRow.BackColor =
            //Color.YellowGreen;
        }

        void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            GridView gv = sender as GridView;
            if (e.Alt)
                switch (e.KeyCode)
                {
                    case Keys.Up:
                        UpDown(1, gv);
                        break;
                    case Keys.Down:
                        UpDown(-1, gv);
                        break;
                }
        }

        private void UpDown(int yon, GridView gv)
        {
            gv.GridControl.Focus();
            int index = gv.FocusedRowHandle;
            if (yon>0 && index <= 0 ||
                yon < 0 && index >= gv.DataRowCount - 1) 
                return;

            DataRow row1 = gv.GetDataRow(index);
            DataRow row2 = gv.GetDataRow(index - yon);
            object val1 = row1[OrderFieldName];
            object val2 = row2[OrderFieldName];
            row1[OrderFieldName] = val2;
            row2[OrderFieldName] = val1;

            gv.FocusedRowHandle = index;
        }


        void bbiKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            //List<int> T = new List<int>();
            //for (int i = 0; i < ds.TaskList.Rows.Count; i++)
            //    T.Add(ds.TaskList[i]["TaskListId"].ToInt());
            //string sT = string.Join(",", T.ToArray());

            //List<int> TL = new List<int>();
            //for (int i = 0; i < ds.TaskListLines.Rows.Count; i++)
            //    TL.Add(ds.TaskListLines[i]["TaskListLinesId"].ToInt());
            //string sTL = string.Join(",", TL.ToArray());

            Save();


            DataTable dt1 = new DataTable("TaskList");
            dt1.Columns.Add("TaskListId", typeof(int));
            dt1.Columns.Add("Sira", typeof(int));
            for (int i = 0; i < ds.TaskList.Rows.Count; i++)
                dt1.Rows.Add(ds.TaskList[i]["TaskListId"], ds.TaskList[i]["Sira"]);
            
            GetDetails("TaskList", "GetTaskList", dt1.Serialize());

        }


    }
}
